package edu.sctu.xialei.May.day20150521one;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 5/21/2015.
 */
public class DatasDealling extends PrinttingDatas {

    public TeacherData[] sortTeacherNumber(TeacherData[] teachers){
        TeacherData[] teachers1 = new TeacherData[teachers.length];

        teachers1 = teachers;

        TeacherData temp;
        for(int i = teachers1.length-1;i>0;--i)
        {
            for(int j = 0;j < i;++j)
            {
                if(teachers1[j+1].numberTeacher < teachers1[j].numberTeacher)
                {
                    temp = teachers1[j];
                    teachers1[j] = teachers1[j+1];
                    teachers1[j+1] = temp;
                }
            }
        }
        return teachers1;
    }



    public TeacherData[] checkTitleOfProfessor (TeacherData[] teachers){
        List teachersList = new ArrayList<>();

        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i].titleTeacher.equals("教授")){
                teachersList.add(teachers[i]);
            }
        }

        TeacherData[] teacher = new TeacherData[teachersList.size()];

        for (int i = 0; i < teacher.length; i++) {
            teacher[i] = (TeacherData) teachersList.get(i);
        }

        return teacher;
    }


    public StudentData[] checkStudentGrade(TeacherData[] teachers,float code){
        List studentlist = new ArrayList<StudentData>();

        for (int i = 0; i < teachers.length; i++) {
            for (int j = 0; j < teachers[i].students.length; j++) {
                if (teachers[i].students[j].gradeStudent > code){
                    studentlist.add(teachers[i].students[j]);
                }
            }
        }

        StudentData []students = new StudentData[studentlist.size()];
        for (int i = 0; i < students.length; i++) {
            students[i] = (StudentData) studentlist.get(i);
        }

        return students;
    }


    public ReturnData checkCourse(TeacherData[] teacherData){

        CourseData courseData = new CourseData();

//        List list = new ArrayList<>();
        List list1 = new ArrayList<StudentData>();
        List list2 = new ArrayList<StudentData>();
        List list3 = new ArrayList<StudentData>();
        List list4 = new ArrayList<StudentData>();

            for (int j = 0; j < teacherData.length; j++) {
                for (int k = 0; k < teacherData[j].students.length; k++) {
                    for (int l = 0; l < teacherData[j].students[k].courseStudent.length; l++) {

                        if (teacherData[j].students[k].courseStudent[l].equals(courseData.courses[0])){
                            list1.add(teacherData[j].students[k]);
                        }
                        if (teacherData[j].students[k].courseStudent[l].equals(courseData.courses[1])){
                            list2.add(teacherData[j].students[k]);
                        }

                        if (teacherData[j].students[k].courseStudent[l].equals(courseData.courses[2])){
                            list3.add(teacherData[j].students[k]);
                        }
                        if (teacherData[j].students[k].courseStudent[l].equals(courseData.courses[3])){
                            list4.add(teacherData[j].students[k]);
                        }
                    }
                }
            }

        return (this.checkMax(list1,list2,list3,list4));
//        Math.max(Math.max(Math.max(list1.size(), list2.size()), list3.size()),list4.size());

    }



    private ReturnData checkMax(List list1,List list2,List list3,List list4){

        List list = new ArrayList<>();

        CourseData cours = new CourseData();
        String courseName = null;

      int max =  Math.max(Math.max(Math.max(list1.size(), list2.size()), list3.size()),list4.size());

        if (max == list1.size()) {
            list = list1;
            courseName = cours.courses[0];
        }
        if (max == list2.size()) {
            courseName = cours.courses[1];
            list = list2;
        }
        if (max == list3.size()) {
            courseName = cours.courses[2];
            list = list3;
        }
        if (max == list4.size()) {
            courseName = cours.courses[3];
            list = list4;
        }

        StudentData[]stu = new StudentData[list.size()];
        for (int i = 0; i < list.size(); i++) {
            stu[i] = (StudentData) list.get(i);
        }

        ReturnData returnData = new ReturnData();
        returnData.stu = stu;
        returnData.courseName = courseName;
        return returnData;

    }
}
